package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DigitalClock;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Generations extends Activity {
	
	connect con = new connect(); 
	ResultSet rs,rs2;
	SimpleAdapter adapter;  
	TextView btn , btn123,spnSize ,CarGroupText,brand;
	Button ok,can;
	EditText newGen;
	String Brand,BrandID,carGroupS,SizeS,New,OID,CarGenCode;
	
	ArrayList<String> size = new ArrayList<String>();
	ArrayList<String> sizeID = new ArrayList<String>();
	ArrayList<String> Group = new ArrayList<String>();
	ArrayList<String> GroupID = new ArrayList<String>();
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.generation_plus);
		
		
		Brand = getIntent().getExtras().getString("gen");
		BrandID = getIntent().getExtras().getString("brandID");
		
		final String BrandsCheck = getIntent().getExtras().getString("BrandsCheck");

		
		ok = (Button) findViewById(R.id.ok);
		can = (Button) findViewById(R.id.cancel);
		brand = (TextView) findViewById(R.id.Brand);
		newGen = (EditText) findViewById(R.id.genPlus);
		
		brand.setText(Brand);
		
		sizeC();
		CarGroupC();
		
		//////////////////////------- gen ID --------//////////////////////////
		try {
		OID = generationOID();
		} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		//////////////////////------- gen ID --------//////////////////////////
		try {
			CarGenCode = generationCode();
		} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}

		
		
		
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
			
				
				New = newGen.getText().toString();
				
				
				 String sql = "INSERT INTO CarGenerations " +
							"(OID,LastModified,ModifiedBy,CreatedDate,CreatedBy,IsActive,IsDeleted,CarGenerationCode" +
							",CarGenerationName,CarBandOID,CarSize,CarGroupOID) " +
							"VALUES (" +
							"'"+OID+"'" + ',' + 
							"GETDATE()" + ',' + 
							"'100201301220001'" + ',' +
							"GETDATE()" + ',' +
							"'100201301220001'" + ',' +
							"'"+1+"'" + ',' +
							"'"+0+"'" + ',' +
							"'"+CarGenCode+"'" + ',' +
							"'"+New+"'" + ',' +
							"'"+BrandID+"'" + ',' +
							"'"+SizeS+"'" + ',' +
							"'"+carGroupS+"'  ) "; 
				 
				
				
				if(con.addData(sql)){	 
				Toast.makeText(Generations.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();
				
				 Intent i = new Intent();
			    	i.putExtra("BrandsCheck", BrandsCheck);
					setResult(InitialServices.REQUEST_CODE, i);
				    finish();

				finish();
				
				}else{
			    Toast.makeText(Generations.this, "Inserted data fail\n"+New+"\n"+BrandID+"\n"+SizeS+"\n"+carGroupS, Toast.LENGTH_SHORT).show();
			    
				}
				
				 
			}
			});
		
		can.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				Intent i = new Intent();
		    	i.putExtra("BrandsCheck", BrandsCheck);
				setResult(InitialServices.REQUEST_CODE, i);
			    finish();
				 
			}
			});
		
		
    }


	//================================== Spinner Size ========================================
	
		private void sizeC() {
			
			final Spinner spn = (Spinner) findViewById(R.id.sizePlus);
			
			
			 rs = con.selectData("SELECT * FROM CarSize ");		   


			 size.add("##### ���͡ Size  #####");	
			 sizeID.add("");
					try {
						while(rs.next()){          
							size.add(rs.getString("CarSizeName"));
							sizeID.add(rs.getString("OID"));
							
							}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}      
				
					ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,size);
					empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
					spn.setAdapter(empolyeeAdapter);
					
					spn.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

			            public void onItemSelected(AdapterView<?> adapterView, 
			          	View view, int i, long l) { 
			          	// TODO Auto-generated method stub
			            	
			            	//carGroup = strSpn.get(i).get("expCard"); 
			            	SizeS = sizeID.get(i);
			            	
			            	}
			            public void onNothingSelected(AdapterView<?> arg0) {
							// TODO Auto-generated method stub
		    				Toast.makeText(Generations.this,
		             				String.valueOf("Your Selected Empty"),
		             					Toast.LENGTH_SHORT).show();
						} 

		      });
				
			}	
	

		//================================== Spinner CarGroup ========================================
	
		private void CarGroupC() {
			
			final Spinner CarGroup = (Spinner) findViewById(R.id.groupPlus);

			
			 rs = con.selectData("SELECT * FROM CarGroup ");		   
		    
			 Group.add("##### ���͡������ͧö  #####");
			 GroupID.add("");
					try {
						while(rs.next()){          
							Group.add(rs.getString("CarGroupName"));
							GroupID.add(rs.getString("OID"));
							;
							}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}      
				
					ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Group);
					empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
					CarGroup.setAdapter(empolyeeAdapter);
					
					CarGroup.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

			            public void onItemSelected(AdapterView<?> adapterView, 
			          	View view, int i, long l) { 
			          	// TODO Auto-generated method stub
			            	
			            	//carGroup = strSpn.get(i).get("expCard"); 
			            	carGroupS = GroupID.get(i);
			            	
			            	
			            	}
			            public void onNothingSelected(AdapterView<?> arg0) {
							// TODO Auto-generated method stub
		  				Toast.makeText(Generations.this,
		           				String.valueOf("Your Selected Empty"),
		           					Toast.LENGTH_SHORT).show();
						} 

		    });
					
			}
	
	
		
		
		
public String generationCode() throws Exception{

		    
		    String StrSql = "SELECT COUNT(CarGenerationCode)+1 FROM CarGenerations  ";


					rs = con.selectData(StrSql);
					
					String No = "";
						
					if(rs.next()){
						
							No = ""+rs.getInt(1);
						
				
						return No;	// Prefix + YYMMDD + XXXX
						
					}else{
						
						return null;
						//return null;
					}
		}


public String generationOID() throws Exception{

	  
    String StrSql = "SELECT COUNT(OID)+1," +
    		  "DAY(GETDATE()), "+
    		  "MONTH(GETDATE()), "+
    		  "CONVERT(VARCHAR(2)," +
    		  "GETDATE(), 11) "+
            "FROM CarGenerations "+
            "WHERE CONVERT(VARCHAR,[CreatedDate], 111) = CONVERT(VARCHAR, GETDATE(), 111) ";
    /* 
	
	 String StrSql = "SELECT COUNT(OID)+1 FROM Colors " +
	 		"WHERE CAST(FLOOR(CAST([CreatedDate] AS FLOAT)) AS DATETIME) = CONVERT(varchar,GETDATE(), 111)";
*/
			rs = con.selectData(StrSql);
			
			String No = "";
			String day = "";
				
			if(rs.next()){
				if(rs.getInt(1) < 10){
					No = "000"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
				}
				else if(rs.getInt(1) < 100){
					No = "00"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else if(rs.getInt(1) < 1000){
					No = "0"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else{
					No = ""+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
			
		
				return "14420"+rs.getInt(4)+rs.getInt(3)+""+day+""+No;	// Prefix + YYMMDD + XXXX
				
				
				
				
			}else{
				
				return null;
				//return null;
			}
}


@Override
public void onBackPressed() {
  
}




}










